home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / base / g.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-28  |  11.4 KB  |  313 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import os.path as os
  7. import ConfigParser
  8. import locale
  9. import pwd
  10. import stat
  11. import re
  12. from codes import *
  13. import logger
  14. log = logger.Logger('', logger.Logger.LOG_LEVEL_INFO, logger.Logger.LOG_TO_CONSOLE)
  15. log.set_level('info')
  16. MINIMUM_PYQT_MAJOR_VER = 3
  17. MINIMUM_PYQT_MINOR_VER = 14
  18. MINIMUM_QT_MAJOR_VER = 3
  19. MINIMUM_QT_MINOR_VER = 0
  20.  
  21. def to_bool(s, default = False):
  22.     if isinstance(s, str) and s:
  23.         if s[0].lower() in ('1', 't', 'y'):
  24.             return True
  25.         if s[0].lower() in ('0', 'f', 'n'):
  26.             return False
  27.     elif isinstance(s, bool):
  28.         return s
  29.     s[0].lower() in ('1', 't', 'y')
  30.     return default
  31.  
  32.  
  33. class Properties(dict):
  34.     
  35.     def __getattr__(self, attr):
  36.         if attr in self.keys():
  37.             return self.__getitem__(attr)
  38.         return ''
  39.  
  40.     
  41.     def __setattr__(self, attr, val):
  42.         self.__setitem__(attr, val)
  43.  
  44.  
  45. prop = Properties()
  46.  
  47. class ConfigBase(object):
  48.     
  49.     def __init__(self, filename):
  50.         self.filename = filename
  51.         self.conf = ConfigParser.ConfigParser()
  52.         self.read()
  53.  
  54.     
  55.     def get(self, section, key, default = u''):
  56.         
  57.         try:
  58.             return self.conf.get(section, key)
  59.         except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
  60.             return default
  61.  
  62.  
  63.     
  64.     def set(self, section, key, value):
  65.         if not self.conf.has_section(section):
  66.             self.conf.add_section(section)
  67.         
  68.         self.conf.set(section, key, value)
  69.         self.write()
  70.  
  71.     
  72.     def sections(self):
  73.         return self.conf.sections()
  74.  
  75.     
  76.     def has_section(self, section):
  77.         return self.conf.has_section(section)
  78.  
  79.     
  80.     def options(self, section):
  81.         return self.conf.options(section)
  82.  
  83.     keys = options
  84.     
  85.     def read(self):
  86.         if self.filename is not None:
  87.             filename = self.filename
  88.             if filename.startswith('/root/'):
  89.                 log.error("attempted to read from '%s'" % self.filename)
  90.                 return None
  91.             
  92.             try:
  93.                 fp = open(self.filename, 'r')
  94.                 self.conf.readfp(fp)
  95.                 fp.close()
  96.             except (OSError, IOError):
  97.                 filename.startswith('/root/')
  98.                 filename.startswith('/root/')
  99.                 log.debug('Unable to open file %s for reading.' % self.filename)
  100.             except:
  101.                 filename.startswith('/root/')<EXCEPTION MATCH>(OSError, IOError)
  102.             
  103.  
  104.         filename.startswith('/root/')
  105.  
  106.     
  107.     def write(self):
  108.         if self.filename is not None:
  109.             filename = self.filename
  110.             if filename.startswith('/root/') or filename.startswith('/etc/'):
  111.                 log.error("attempted to write to '%s'" % self.filename)
  112.                 return None
  113.             
  114.             try:
  115.                 fp = open(self.filename, 'w')
  116.                 self.conf.write(fp)
  117.                 fp.close()
  118.             except (OSError, IOError):
  119.                 filename.startswith('/etc/')
  120.                 filename.startswith('/etc/')
  121.                 log.debug('Unable to open file %s for writing.' % self.filename)
  122.             except:
  123.                 filename.startswith('/etc/')<EXCEPTION MATCH>(OSError, IOError)
  124.             
  125.  
  126.         filename.startswith('/etc/')
  127.  
  128.  
  129.  
  130. class SysConfig(ConfigBase):
  131.     
  132.     def __init__(self):
  133.         ConfigBase.__init__(self, '/etc/hp/hplip.conf')
  134.  
  135.  
  136.  
  137. class State(ConfigBase):
  138.     
  139.     def __init__(self):
  140.         ConfigBase.__init__(self, '/var/lib/hp/hplip.state')
  141.  
  142.  
  143.  
  144. class UserConfig(ConfigBase):
  145.     
  146.     def __init__(self):
  147.         if not os.geteuid() == 0:
  148.             prop.user_dir = os.path.expanduser('~/.hplip')
  149.             
  150.             try:
  151.                 if not os.path.exists(prop.user_dir):
  152.                     os.makedirs(prop.user_dir)
  153.             except OSError:
  154.                 pass
  155.  
  156.             prop.user_config_file = os.path.join(prop.user_dir, 'hplip.conf')
  157.             if not os.path.exists(prop.user_config_file):
  158.                 
  159.                 try:
  160.                     file(prop.user_config_file, 'w').close()
  161.                     s = os.stat(os.path.dirname(prop.user_config_file))
  162.                     os.chown(prop.user_config_file, s[stat.ST_UID], s[stat.ST_GID])
  163.                 except IOError:
  164.                     pass
  165.                 except:
  166.                     None<EXCEPTION MATCH>IOError
  167.                 
  168.  
  169.             None<EXCEPTION MATCH>IOError
  170.             ConfigBase.__init__(self, prop.user_config_file)
  171.         else:
  172.             prop.user_dir = None
  173.             prop.user_config_file = None
  174.             ConfigBase.__init__(self, None)
  175.  
  176.     
  177.     def workingDirectory(self):
  178.         t = self.get('last_used', 'working_dir', os.path.expanduser('~'))
  179.         
  180.         try:
  181.             t = t.decode('utf-8')
  182.         except UnicodeError:
  183.             log.error('Invalid unicode: %s' % t)
  184.  
  185.         log.debug('working directory: %s' % t)
  186.         return t
  187.  
  188.     
  189.     def setWorkingDirectory(self, t):
  190.         self.set('last_used', 'working_dir', t.encode('utf-8'))
  191.         log.debug('working directory: %s' % t.encode('utf-8'))
  192.  
  193.  
  194. os.umask(31)
  195. sys_conf = SysConfig()
  196. sys_state = State()
  197. user_conf = UserConfig()
  198.  
  199. try:
  200.     (prop.locale, prop.encoding) = locale.getdefaultlocale()
  201. except ValueError:
  202.     prop.locale = 'en_US'
  203.     prop.encoding = 'UTF8'
  204.  
  205. prop.version = sys_conf.get('hplip', 'version', '0.0.0')
  206. _p = re.compile('(\\d\\w*)', re.I)
  207. _x = []
  208. for _y in prop.version.split('.')[:3]:
  209.     _z = _p.match(_y)
  210.     if _z is not None:
  211.         _x.append(_z.group(1))
  212.         continue
  213.  
  214. prop.installed_version = '.'.join(_x)
  215.  
  216. try:
  217.     prop.installed_version_int = []([]([ '%02x' % int(_y) for _y in _x ]), 16)
  218. except ValueError:
  219.     prop.installed_version_int = 0
  220.  
  221. prop.home_dir = sys_conf.get('dirs', 'home', os.path.realpath(os.path.normpath(os.getcwd())))
  222. prop.username = pwd.getpwuid(os.getuid())[0]
  223. pdb = pwd.getpwnam(prop.username)
  224. prop.userhome = pdb[5]
  225. prop.history_size = 50
  226. prop.data_dir = os.path.join(prop.home_dir, 'data')
  227. prop.image_dir = os.path.join(prop.home_dir, 'data', 'images')
  228. prop.xml_dir = os.path.join(prop.home_dir, 'data', 'xml')
  229. prop.models_dir = os.path.join(prop.home_dir, 'data', 'models')
  230. prop.localization_dir = os.path.join(prop.home_dir, 'data', 'localization')
  231. prop.max_message_len = 8192
  232. prop.max_message_read = 65536
  233. prop.read_timeout = 90
  234. prop.ppd_search_path = '/usr/share;/usr/local/share;/usr/lib;/usr/local/lib;/usr/libexec;/opt;/usr/lib64'
  235. prop.ppd_search_pattern = 'HP-*.ppd.*'
  236. prop.ppd_download_url = 'http://www.linuxprinting.org/ppd-o-matic.cgi'
  237. prop.ppd_file_suffix = '-hpijs.ppd'
  238. prop.gui_build = to_bool(sys_conf.get('configure', 'gui-build', '0'))
  239. prop.net_build = to_bool(sys_conf.get('configure', 'network-build', '0'))
  240. prop.par_build = to_bool(sys_conf.get('configure', 'pp-build', '0'))
  241. prop.usb_build = True
  242. prop.scan_build = to_bool(sys_conf.get('configure', 'scanner-build', '0'))
  243. prop.fax_build = to_bool(sys_conf.get('configure', 'fax-build', '0'))
  244. prop.doc_build = to_bool(sys_conf.get('configure', 'doc-build', '0'))
  245. prop.foomatic_xml_install = to_bool(sys_conf.get('configure', 'foomatic-xml-install', '0'))
  246. prop.foomatic_ppd_install = to_bool(sys_conf.get('configure', 'foomatic-ppd-install', '0'))
  247. prop.hpcups_build = to_bool(sys_conf.get('configure', 'hpcups-install', '0'))
  248. prop.hpijs_build = to_bool(sys_conf.get('configure', 'hpijs-install', '0'))
  249. spinner = '\\|/-\\|/-'
  250. spinpos = 0
  251.  
  252. def update_spinner():
  253.     global spinpos
  254.     if not log.is_debug() and sys.stdout.isatty():
  255.         sys.stdout.write('\x08' + spinner[spinpos])
  256.         spinpos = (spinpos + 1) % 8
  257.         sys.stdout.flush()
  258.     
  259.  
  260.  
  261. def cleanup_spinner():
  262.     if not log.is_debug() and sys.stdout.isatty():
  263.         sys.stdout.write('\x08 \x08')
  264.         sys.stdout.flush()
  265.     
  266.  
  267. ERROR_STRINGS = {
  268.     ERROR_SUCCESS: 'No error',
  269.     ERROR_UNKNOWN_ERROR: 'Unknown error',
  270.     ERROR_DEVICE_NOT_FOUND: 'Device not found',
  271.     ERROR_INVALID_DEVICE_ID: 'Unknown/invalid device-id field',
  272.     ERROR_INVALID_DEVICE_URI: 'Unknown/invalid device-uri field',
  273.     ERROR_DATA_LENGTH_EXCEEDS_MAX: 'Data length exceeds maximum',
  274.     ERROR_DEVICE_IO_ERROR: 'Device I/O error',
  275.     ERROR_NO_PROBED_DEVICES_FOUND: 'No probed devices found',
  276.     ERROR_DEVICE_BUSY: 'Device busy',
  277.     ERROR_DEVICE_STATUS_NOT_AVAILABLE: 'DeviceStatus not available',
  278.     ERROR_INVALID_SERVICE_NAME: 'Invalid service name',
  279.     ERROR_ERROR_INVALID_CHANNEL_ID: 'Invalid channel-id (service name)',
  280.     ERROR_CHANNEL_BUSY: 'Channel busy',
  281.     ERROR_DEVICE_DOES_NOT_SUPPORT_OPERATION: 'Device does not support operation',
  282.     ERROR_DEVICEOPEN_FAILED: 'Device open failed',
  283.     ERROR_INVALID_DEVNODE: 'Invalid device node',
  284.     ERROR_INVALID_HOSTNAME: 'Invalid hostname ip address',
  285.     ERROR_INVALID_PORT_NUMBER: 'Invalid JetDirect port number',
  286.     ERROR_NO_CUPS_QUEUE_FOUND_FOR_DEVICE: 'No CUPS queue found for device.',
  287.     ERROR_DATFILE_ERROR: 'DAT file error',
  288.     ERROR_INVALID_TIMEOUT: 'Invalid timeout',
  289.     ERROR_IO_TIMEOUT: 'I/O timeout',
  290.     ERROR_FAX_INCOMPATIBLE_OPTIONS: 'Incompatible fax options',
  291.     ERROR_FAX_INVALID_FAX_FILE: 'Invalid fax file',
  292.     ERROR_FAX_FILE_NOT_FOUND: 'Fax file not found',
  293.     ERROR_INTERNAL: 'Unknown internal error' }
  294.  
  295. class Error(Exception):
  296.     
  297.     def __init__(self, opt = ERROR_INTERNAL):
  298.         self.opt = opt
  299.         self.msg = ERROR_STRINGS.get(opt, ERROR_STRINGS[ERROR_INTERNAL])
  300.         log.debug('Exception: %d (%s)' % (opt, self.msg))
  301.         Exception.__init__(self, self.msg, opt)
  302.  
  303.  
  304.  
  305. try:
  306.     True
  307. except NameError:
  308.     True = 1 == 1
  309.     False = not True
  310.  
  311. supported_locales = {
  312.     'en_US': ('us', 'en', 'en_us', 'american', 'america', 'usa', 'english') }
  313.